# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : plugin_manager_view.py
# Time       ：2023/10/28 15:58
# Author     ：YangYong
# version    ：python 3.10
# Description：插件管理视图类
"""
from flask_restx import fields

from apis.plugin import plugin_ns
from apis.plugin.models.plugin_manager import PluginManager
from apis.plugin.schema.plugin_manager_schema import PluginManagerSchema
from public.api_utils.api_decorator import handle_api_error
from public.base_response import MarshalModel
from public.base_view import BaseView, BaseDetailsView

plugin_manager = {
    'id': fields.String(description='插件管理ID'),
    'model_name': fields.String(description='插件模型名称'),
    'file_name': fields.String(description='插件文件名'),
    'desc': fields.String(description='插件描述'),
    'enable': fields.Boolean(description='是否启用'),
}

plugin_manager_marshal = MarshalModel('plugin_manager', plugin_ns)


class PluginMangerDetailView(BaseDetailsView):
    url = '/manager/detail'

    def __init__(self, *args, **kwargs):
        super(PluginMangerDetailView, self).__init__(*args, **kwargs)
        self.table_orm = PluginManager
        self.table_schema = PluginManagerSchema

    @handle_api_error()
    @plugin_ns.doc(description="获取插件管理详情")
    @plugin_ns.expect(plugin_manager_marshal.detail_request())
    @plugin_ns.marshal_with(plugin_manager_marshal.detail_response(plugin_manager))
    def get(self):
        """获取插件管理详情"""
        return super(PluginMangerDetailView, self).get()


class PluginManagerView(BaseView):
    url = '/manager'

    def __init__(self, *args, **kwargs):
        super(PluginManagerView, self).__init__(*args, **kwargs)
        self.table_orm = PluginManager
        self.table_schema = PluginManagerSchema
        self.is_logger = True

    @handle_api_error()
    @plugin_ns.doc(description='获取插件列表')
    @plugin_ns.expect(plugin_manager_marshal.list_request(plugin_manager))
    @plugin_ns.marshal_with(plugin_manager_marshal.list_response(plugin_manager))
    def get(self):
        """获取插件列表"""
        return super(PluginManagerView, self).get()

    @handle_api_error()
    @plugin_ns.doc(description='创建插件管理')
    @plugin_ns.expect(plugin_manager_marshal.create_request(plugin_manager))
    @plugin_ns.marshal_with(plugin_manager_marshal.create_response())
    def post(self):
        """创建插件管理"""
        return super(PluginManagerView, self).post()

    @handle_api_error()
    @plugin_ns.doc(description='修改插件管理')
    @plugin_ns.expect(plugin_manager_marshal.update_request(plugin_manager))
    @plugin_ns.marshal_with(plugin_manager_marshal.update_response())
    def put(self):
        """修改插件管理"""
        return super(PluginManagerView, self).put()

    @handle_api_error()
    @plugin_ns.doc(description='删除插件管理')
    @plugin_ns.expect(plugin_manager_marshal.delete_request())
    @plugin_ns.marshal_with(plugin_manager_marshal.delete_response())
    def delete(self):
        """删除插件管理"""
        return super(PluginManagerView, self).delete()
